/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

function LogViewModel()
{

    

    var self = this;
	// put a property loop here
	self.LogID = ko.observable();
			self.Priority = ko.observable();
			self.Severity = ko.observable();
			self.Title = ko.observable();
			self.MachineName = ko.observable();
			self.AppDomainName = ko.observable();
			self.Message = ko.observable();
			self.TS = ko.observable();
			self.CreatedBy = ko.observable();
			self.CreatedDt = ko.observable();
			self.ModifiedBy = ko.observable();
			self.ModifiedDt = ko.observable();
			self.ApplicationID = ko.observable();
			self.LogCategoryID = ko.observable();
			self.Action = ko.observable();
			self.Caller = ko.observable();
			self.Role = ko.observable();
			self.ApplicationName_s = ko.observable('');
			self.LogCategoryName_s = ko.observable('');
			self.Message_s = ko.observable('');
			self.filterText = ko.observable('');
			
			self.PageSize = ko.observable(10);
			self.PageNumber = ko.observable(1);
			self.SortColumn = ko.observable();
			self.SortType = ko.observable();
			self.TotalRows = ko.observable();
			self.TotalRowsCal = ko.observableArray();
			self.PageNumbers = ko.observableArray([]);

			self.sortRecords = ko.observableArray();
			self.isActive= ko.observable(false);
			
    
    self.LogList = ko.observableArray([]);
    self.setLog = function (
			LogID, Priority, Severity, Title, MachineName, AppDomainName, Message, TS, CreatedBy, CreatedDt, ModifiedBy, ModifiedDt, ApplicationID, LogCategoryID, _Action, _Caller, _Role, _PageNumber, _PageSize, _SortColumn, _SortType, _TotalRows 
	
	) {

	LogID = _LogID;
			Priority = _Priority;
			Severity = _Severity;
			Title = _Title;
			MachineName = _MachineName;
			AppDomainName = _AppDomainName;
			Message = _Message;
			TS = _TS;
			CreatedBy = _CreatedBy;
			CreatedDt = _CreatedDt;
			ModifiedBy = _ModifiedBy;
			ModifiedDt = _ModifiedDt;
			ApplicationID = _ApplicationID;
			LogCategoryID = _LogCategoryID;
			Action = _Action;
			Caller = _Caller;
			Role = _Role;
			PageSize = _PageSize
			PageNumber = _PageNumber;
			SortColumn = _SortColumn;
			SortType = _SortType;
			TotalRows = _TotalRows;
        
    }; //  End of set procedure 

    //filter
    self.queryResult = ko.observableArray();
   
   
    self.gridViewModel = new ko.simpleGrid.viewModel({
        
        data: self.queryResult,
        columns: [
            { headerText: "Title", rowText: "Title" },
            { headerText: "Application Name", rowText: "LogID" },
            { headerText: "Category", rowText: "LogCategoryID" }
        ],
        pageSize: 20
    });


    






	self.LogList = ko.observableArray([]);
	self.filter = ko.observable();

	self.search = function () {
	    //alert('we are in filter');
	    //self.filterText(self.filter().toLowerCase());
	    var filterText = self.filter().toLowerCase();
	    self.PageNumber(1);

	    self.ApplicationName_s = filterText;
	    self.LogCategoryName_s = filterText;
	    self.Message_s = filterText;
	    //alert(filterText);
	    self.GetLoggingByParameter();

	}

	self.searchKeyboardCmd = function (data, event) {
	    if (event.keyCode == 13) {
	        
	        self.search();
	    }
	    return true;
	}


	self.GetLoggingByParameter = function () {
	    var filldata=self.LogList;
	    self.Action = "GetAll";
	    self.Caller = "trooks";
	    self.Role = "user";
	    //self.PageNumber = 1;
	    //self.PageSize = 5;
	    self.SortColumn = "LogID";
	    self.SortType = "asc";
	    jQuery.ajax({

	        url: "./api/LogApi/",
	        data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType,ApplicationName:self.ApplicationName_s,LogCategoryName:self.LogCategoryName_s,Message:self.Message_s },
	        type: 'GET',
	        success: function (result) {
	            
	            self.LogList.removeAll();
	            self.TotalRowsCal.removeAll();
	            $.each(result, function (key, val) {
	                self.TotalRowsCal.push(val.TotalRows);
	                self.queryResult.push(val);
	                self.LogList.push(
                        val
                    )
	            })
	            self.create_pages();
	            
	        },
	        error: function (xhr, ajaxOptions, thrownError) {
	            $('#error-dis').text(thrownError);
	            $('#register-error').css({ 'display': 'block' });
	            $('#SearchFilter-home').css({ 'display': 'none' });
	            $(document).scrollTop(0);
	        }
	    });

	   
	};

	ko.bindingHandlers.bootstrapPopover = {
	    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
	        var options = valueAccessor();
	        var defaultOptions = { trigger: 'click' };
	        options = $.extend(true, {}, defaultOptions, options);
	        $(element).popover(options);
	    }
	};


	



	
	ko.bindingHandlers.dateString = {
	    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
	        var value = valueAccessor();
	        var valueUnwrapped = ko.utils.unwrapObservable(value);
	        var dateValue = new Date(valueUnwrapped);
	        var pattern = 'MM/dd/yyyy';

            //can change the format using other functions
	        $(element).text(dateValue.toUTCString());
	    }
	}

    //Pagination 


	self.totalPages = ko.computed(function () {
	    var div = Math.floor(self.TotalRowsCal()[0] / self.PageSize());
	    div += self.TotalRowsCal()[0] % self.PageSize() > 0 ? 1 : 0;
	    return div;
	});

	this.paginated = ko.computed(function () {
	    var first = self.PageNumber() * self.PageSize;
	    return self.LogList.slice(first, first + self.PageSize);
	});
	this.create_pages = function () {
	    var pages = parseInt(self.totalPages());
	    var pNumbers = new Array();
	    self.PageNumbers.removeAll();
	    for (var i = 1; i <= pages; i++) {
	        //$('#loading-indicator').hide();
	        $('#loading-indicator2').css({ "z-index": "3", "display": "none" });
	        pNumbers.push(i);
	        
	       
	    }

	    self.PageNumbers(pNumbers);
	}


	this.hasPrevious = ko.computed(function () {
	    return self.PageNumber() != 1;
	});
	this.hasNext = ko.computed(function () {
	    return self.PageNumber() != self.totalPages();
	});
	this.next = function () {
	    if (self.PageNumber() < self.totalPages()) {
	        self.PageNumber(self.PageNumber() + 1);
	        self.GetLoggingByParameter();
	    }
	    
	}

	this.previous = function () {
	    if (self.PageNumber() != 1) {
	        self.PageNumber(self.PageNumber() - 1);
	        self.GetLoggingByParameter();
	    }
	}
	this.pageClick = function (pageSelected) {
	    self.PageNumber(pageSelected);
	    self.GetLoggingByParameter();
	    
	    
	}

	



	ko.bindingHandlers.sort = {
	    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
	        var asc = false;
	        element.style.cursor = 'pointer';

	        element.onclick = function () {
	            var value = valueAccessor();
	            var prop = value.prop;
	            var data = value.arr;

	            asc = !asc;
	            if (asc) {
	                self.LogList.sort(function (left, right) {
	                    return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
	                });
	            } else {
	                self.LogList.sort(function (left, right) {
	                    return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
	                });
	            }
	        }
	    }
	};









}; //End of view model





